using SqlSugar;
using zhwy.saas.api.Extensions.Log;
using zhwy.saas.api.Repository;

namespace zhwy.saas.api.Extensions
{
    public static class SqlSugarSetup
    {
        public static void AddSqlSugarSetup(this IServiceCollection services, List<ConnectionConfig> dbConfigs)
        {
            // 注册 SqlSugar 客户端
            services.AddScoped<ISqlSugarClient>(sp =>
            {
                var logService = sp.GetService<ILogExt>();

                // 创建数据库对象
                var db = new SqlSugarClient(dbConfigs);

                // 自动创建和更新表结构
                foreach (var connectionConfig in dbConfigs)
                {
                    var connection = db.GetConnection(connectionConfig.ConfigId);

                    //// 自动创建/更新表
                    //connection.DbMaintenance.CreateDatabase();
                    //connection.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(RequestLogEntity));


                    // ... 其他配置保持不变 ...
                    connection.Aop.OnLogExecuting = (sql, parameters) =>
                    {
                        logService?.Debug($"数据库[{connectionConfig.ConfigId}]执行SQL：{sql}");
                    };

                    connection.Aop.OnError = (ex) =>
                    {
                        logService?.Error(ex, $"数据库[{connectionConfig.ConfigId}]执行SQL出错");
                    };

                    connection.Aop.OnLogExecuted = (sql, parameters) =>
                    {
                        if (connection.Ado.SqlExecutionTime.TotalMilliseconds > 1000)
                        {
                            logService?.Warn($"数据库[{connectionConfig.ConfigId}]执行SQL耗时过长: {connection.Ado.SqlExecutionTime.TotalMilliseconds}ms");
                        }
                    };


                }

                return db;
            });

            // 注册通用仓储
            services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
        }

        public static List<ConnectionConfig> CreateConnections(IConfiguration configuration)
        {
            var res = new List<ConnectionConfig>
            {
                new ConnectionConfig
                {
                    ConfigId = "mysql_zhwy_db",
                    ConnectionString = configuration.GetConnectionString("mysql_zhwy_db"),
                    DbType = DbType.MySql,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute,
                    SlaveConnectionConfigs = new List<SlaveConnectionConfig>
                    {
                        //new SlaveConnectionConfig
                        //{
                        //    ConnectionString = configuration.GetConnectionString("MainDb_Slave1"),
                        //    HitRate = 10
                        //},
                        //new SlaveConnectionConfig
                        //{
                        //    ConnectionString = configuration.GetConnectionString("MainDb_Slave2"),
                        //    HitRate = 10
                        //}
                    },
                    
                },
            };
            return res;
        }
    }
}